<?php

$allowedSizes = array (640,480,400,300,320,240,100,75,50,40,38,24);

$x = (int) $_GET['x'];
$y = (int) $_GET['y'];
$sImagePath = './' . trim ($_GET['url'], '/');

if (!file_exists ($sImagePath)) {
	header('Status: 404 Not Found', true, 404);
	header('HTTP/1.0 404 Not Found', true, 404);
	exit;
}

$md5Key = md5 ($x . $y . $sImagePath);

$thumbPath = './images/thumbs/' . $md5Key;

$typesAssoc = array (1=>'gif',2=>'jpeg',3=>'png');

if (file_exists ($thumbPath)) {
	if (filemtime ($thumbPath) > filemtime ($sImagePath)) {
		$info = getimagesize ($thumbPath);
		
		if (!isset ($typesAssoc[$info[2]])) {
			header('Status: 404 Not Found', true, 404);
			header('HTTP/1.0 404 Not Found', true, 404);
			exit;
		}
		
		$fmtime = filemtime ($thumbPath);
		
		$f = fopen($thumbPath, 'rb');
				
		header("Content-Type: image/" . $typesAssoc[$info[2]]);
		header("Content-Length: " . filesize($thumbPath));
		header("Last-Modified: " . gmdate("D, d M Y H:i:s", $fmtime) . " GMT");
		header("Expires: " . gmdate("D, d M Y H:i:s", $fmtime + 604800) . " GMT");
		
		fpassthru($f);
	
		exit;
	} else {
		unlink ($thumbPath);
	}
}

$info = getimagesize ($sImagePath);

if (!isset ($typesAssoc[$info[2]]) || !in_array ($x,$allowedSizes) || !in_array ($y,$allowedSizes)) {
	header('Status: 404 Not Found', true, 404);
	header('HTTP/1.0 404 Not Found', true, 404);
	exit;
}

$type = $typesAssoc[$info[2]];

switch ($type) {
	case 'gif' : {
		$thumbbase = imagecreatefromgif ($sImagePath);
		break;
	}
	case 'jpeg' : {
		$thumbbase = imagecreatefromjpeg ($sImagePath);
		break;
	}
	case 'png' : {
		$thumbbase = imagecreatefrompng ($sImagePath);
		break;
	}
}

$thumb = imagecreatetruecolor ($x, $y);

$whiteColor = imagecolorallocate($thumb, 255, 255, 255);
imagefill ($thumb, 0, 0, $whiteColor);

$realX = imagesx ($thumbbase);
$realY = imagesy ($thumbbase);

if ($realX > $x || $realY > $y) {
	$k = max ($realX/$x, $realY/$y);
	$sizedX = (int) $realX / $k;
	$sizedY = (int) $realY / $k; 
} else {
	$sizedX = $x;
	$sizedY = $y;
}

$outX = $x;
$outY = $y;

$paddingLeft = 0;
$paddingTop = 0;

if ($sizedX < $x && $sizedY < $y) {
	$paddingTop = (int) round (($y - $sizedY) / 2);
	$paddingLeft = (int) round (($x - $sizedX) / 2);
}
if ($sizedY < $y) {
	$outY = $sizedY;
	$paddingTop = (int) round (($y - $sizedY) / 2);
}
if ($sizedX < $x) {
	$outX = $sizedX;
	$paddingLeft = (int) round (($x - $sizedX) / 2);
}

imagecopyresampled ($thumb, $thumbbase, $paddingLeft, $paddingTop, 0, 0, $outX, $outY, $realX, $realY);

header("Content-Type: image/" . $type);
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Expires: " . gmdate("D, d M Y H:i:s", time() + 604800) . " GMT");

switch ($type) {
	case 'gif' : {
		imagegif ($thumb, $thumbPath);
		imagegif ($thumb);
		break;
	}
	case 'jpeg' : {
		imagejpeg($thumb, $thumbPath, 70);
		imagejpeg($thumb);
		break;
	}
	case 'png' : {
		imagepng ($thumb, $thumbPath);
		imagepng ($thumb);
		break;
	}
}
		
imagedestroy ($thumb);
imagedestroy ($thumbbase);